/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FlowSimulatorPanel.java
 *
 * Created on 7-okt-2009, 19:54:48
 */

package gui;

import flowsimulation.FlowSimulator;
import flowvis.DensityRenderer;
import flowvis.VelocityRenderer;
import flowvis.ForceRenderer;

/**
 *
 * @author michel
 */
public class FlowSimulatorPanel extends javax.swing.JPanel {

    FlowSimulator simulator;

    /**
     * Constructor. Creates new form FlowSimulatorPanel
     * @param sim , initialized FlowSimulator.
     */
    public FlowSimulatorPanel(FlowSimulator sim) {
        initComponents();
        simulator = sim;
        viscosityTextfield.setText("" + simulator.getViscosity());
        timestepTextField.setText("" + simulator.getTimeStep());
        densityButton.setSelected( true );
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        dataSetButtonGroup = new javax.swing.ButtonGroup();
        runningCheckbox = new javax.swing.JCheckBox();
        viscosityTextfield = new javax.swing.JTextField();
        timestepTextField = new javax.swing.JTextField();
        viscosityLabel = new javax.swing.JLabel();
        timeStepLabel = new javax.swing.JLabel();
        timeStepSliderVariable = new javax.swing.JSlider();
        viscositySliderVariable = new javax.swing.JSlider();
        dataSetLabel = new javax.swing.JLabel();
        densityButton = new javax.swing.JRadioButton();
        velocityMButton = new javax.swing.JRadioButton();
        ForceMagnitude = new javax.swing.JRadioButton();

        runningCheckbox.setSelected(true);
        runningCheckbox.setText("Running");
        runningCheckbox.setToolTipText("Start/stop simulation");
        runningCheckbox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                runningCheckboxActionPerformed(evt);
            }
        });

        viscosityTextfield.setEditable(false);

        timestepTextField.setEditable(false);

        viscosityLabel.setLabelFor(viscosityTextfield);
        viscosityLabel.setText("Viscosity:");

        timeStepLabel.setLabelFor(timestepTextField);
        timeStepLabel.setText("Time step:");
        timeStepLabel.setName("Time_Step"); // NOI18N

        timeStepSliderVariable.setToolTipText("Drag to change the time step");
        timeStepSliderVariable.setValue(40);
        timeStepSliderVariable.setName("Time_Step_Slide"); // NOI18N
        timeStepSliderVariable.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                timeStepSliderVariableMouseDragged(evt);
            }
        });

        viscositySliderVariable.setMinimum(1);
        viscositySliderVariable.setToolTipText("Drag to change the viscosity");
        viscositySliderVariable.setValue(1);
        viscositySliderVariable.setName("Viscosity_slide"); // NOI18N
        viscositySliderVariable.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                viscositySliderVariableMouseDragged(evt);
            }
        });

        dataSetLabel.setText("Data set:");

        dataSetButtonGroup.add(densityButton);
        densityButton.setText("Density");
        densityButton.setToolTipText("Choose as data set to visualize");
        densityButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                densityButtonActionPerformed(evt);
            }
        });

        dataSetButtonGroup.add(velocityMButton);
        velocityMButton.setText("Velocity magnitude");
        velocityMButton.setToolTipText("Choose as data set to visualize");
        velocityMButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                velocityMButtonActionPerformed(evt);
            }
        });

        dataSetButtonGroup.add(ForceMagnitude);
        ForceMagnitude.setText("Force magnitude");
        ForceMagnitude.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ForceMagnitudeButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(runningCheckbox)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(viscosityLabel)
                            .addComponent(timeStepLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(viscosityTextfield, javax.swing.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE)
                            .addComponent(timestepTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE)
                            .addComponent(viscositySliderVariable, 0, 0, Short.MAX_VALUE)
                            .addComponent(timeStepSliderVariable, 0, 0, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(dataSetLabel)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(densityButton)
                            .addComponent(ForceMagnitude)
                            .addComponent(velocityMButton))))
                .addContainerGap(176, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(runningCheckbox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(viscosityLabel)
                    .addComponent(viscosityTextfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(7, 7, 7)
                .addComponent(viscositySliderVariable, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(timestepTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(timeStepLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(timeStepSliderVariable, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(dataSetLabel)
                    .addComponent(densityButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(velocityMButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(ForceMagnitude)
                .addContainerGap(192, Short.MAX_VALUE))
        );

        timeStepSliderVariable.getAccessibleContext().setAccessibleDescription("");
        viscositySliderVariable.getAccessibleContext().setAccessibleDescription("");
        dataSetLabel.getAccessibleContext().setAccessibleName("Data set");
        ForceMagnitude.getAccessibleContext().setAccessibleDescription("Choose as data set to visualize");

        getAccessibleContext().setAccessibleParent(getRootPane());
    }// </editor-fold>//GEN-END:initComponents

    private void runningCheckboxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_runningCheckboxActionPerformed
        simulator.setPaused(!runningCheckbox.isSelected());
    }//GEN-LAST:event_runningCheckboxActionPerformed

    private void timeStepSliderVariableMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_timeStepSliderVariableMouseDragged
        double v = 0.01 * ((double)timeStepSliderVariable.getValue());
        simulator.setTimeStep( v );
        timestepTextField.setText( String.valueOf(v) );
    }//GEN-LAST:event_timeStepSliderVariableMouseDragged

    private void viscositySliderVariableMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_viscositySliderVariableMouseDragged
        double v = 0.001 * ((double)viscositySliderVariable.getValue());
        simulator.setViscosity( v );
        viscosityTextfield.setText( String.valueOf(v) );
    }//GEN-LAST:event_viscositySliderVariableMouseDragged

    private void velocityMButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_velocityMButtonActionPerformed
        VelocityRenderer.setVisible( true );
        DensityRenderer.setVisible( false );
        ForceRenderer.setVisible(false);
    }//GEN-LAST:event_velocityMButtonActionPerformed

    private void densityButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_densityButtonActionPerformed
        VelocityRenderer.setVisible( false );
        DensityRenderer.setVisible( true );
        ForceRenderer.setVisible(false);
    }//GEN-LAST:event_densityButtonActionPerformed

private void ForceMagnitudeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ForceMagnitudeButtonActionPerformed
        VelocityRenderer.setVisible( false );
        DensityRenderer.setVisible( false);
        ForceRenderer.setVisible( true );
}//GEN-LAST:event_ForceMagnitudeButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton ForceMagnitude;
    private javax.swing.ButtonGroup dataSetButtonGroup;
    private javax.swing.JLabel dataSetLabel;
    private javax.swing.JRadioButton densityButton;
    private javax.swing.JCheckBox runningCheckbox;
    private javax.swing.JLabel timeStepLabel;
    private javax.swing.JSlider timeStepSliderVariable;
    private javax.swing.JTextField timestepTextField;
    private javax.swing.JRadioButton velocityMButton;
    private javax.swing.JLabel viscosityLabel;
    private javax.swing.JSlider viscositySliderVariable;
    private javax.swing.JTextField viscosityTextfield;
    // End of variables declaration//GEN-END:variables

}
